/**
 * Java核心技术
 */
package com.tx.core.basic;

import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

/**
 * 
 * @author wanghy
 * @since 2013-2-17
 * 
 */
@SuppressWarnings("unused")
public class Basic {

	private void StrictMath() {
	}

	/**
	 * 测试Double
	 */
	private static void testDouble() {
		System.out.println(Double.POSITIVE_INFINITY);//正无穷大
		System.out.println(Double.NEGATIVE_INFINITY);//负无穷大
		System.out.println(Double.NaN);
		Double x = Double.NaN;
		System.out.println(x == Double.NaN);//不能这样检测
		System.out.println(Double.isNaN(x));//应该这样检测
	}
	
	/**
	 * 测试Char
	 */
	private static void testChar() {
		System.out.println("\u03c0");//unicode
	}
	
	//注意：const 是Java中的保留字
	
	/**
	 * 测试运算符
	 */
	private static void testOperator() {
//		System.out.println(-3/0);//整数除以0会抛异常
		System.out.println(-3.2/0);//浮点数除以0会得到无穷大或NaN
		
		//注意：strictfp 是Java中的关键字，表示严格进行浮点数计算
	}
	
	/**
	 * 测试String
	 */
	private static void testString () {
		
		String greeting = "Hello";
		System.out.println(greeting.length());
		
		int cpCount = greeting.codePointCount(0, greeting.length());//返回此 String 的指定文本范围中的 Unicode 代码点数
		System.out.println(cpCount);
		
		int index = greeting.offsetByCodePoints(0, 3);//返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引
		int cp = greeting.codePointAt(index);//返回指定索引处的字符（Unicode 代码点）
		System.out.println(cp);
		
		//If Z is UTF-16,so result is Z,not is space.So not use char type.
		String sentence = "Z is the set of integers";
		char ch = sentence.charAt(1);
		System.out.println(ch);
		
		//构建字符串：多线程用StringBuffer，单线程用StringBuilder
	}
	
	/**
	 * Test input.
	 */
	private static void testInput() {
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("What's your name?");
		String name = scanner.nextLine();
		
		System.out.println("How old are you?");
		int age = scanner.nextInt();
		
		System.out.println("Hello,"+name+".You are "+age+" years old.");
		
		//Format output.
		System.out.printf("Hello, %s.Next year, you'll be %d", name, age);
		
		//Format String.
		String message = String.format("Hello, %s.Next year, you'll be %d", name, age);
		//Input password.
		//Console only read line,but not read word or number.
		//Eclipse not have console.
		/*Console cons = System.console();
		String username = cons.readLine("User name:");
		char[] password = cons.readPassword("Password:");
		System.out.println(username);
		for (char  ch : password) {
			System.out.print(ch);
		}*/
	}
	
	/**
	 * Test format output.
	 */
	private static void testFormatOutput() {
		double x = -10000.0/3.0;
		System.out.println(x);
		System.out.printf("%8.2f", x);
		System.out.println();
		System.out.printf("%,.2f", x);
		System.out.println();
		System.out.printf("%(.2f", x);
		System.out.println();
		System.out.printf("%tF", new Date());
		System.out.println();
		System.out.printf("%1$s %2$tB %2$te %2$tY", "Due date:", new Date());
		System.out.println();
		System.out.printf("%s %tB %<te %<tY", "Due date:", new Date());
	}
	
	/**
	 * Test file input and output.
	 */
	private static void testFileInputOutput() {
		//Get user current working directory.
		System.out.println("Curr path:" + System.getProperty("user.dir"));
		PrintWriter out = null;
		try {
			//Read file.
			Scanner scanner = new Scanner(new File("d:/test/Test.java"));
			while (scanner.hasNext()) {
				System.out.println(scanner.nextLine());
			}
			//
			out = new PrintWriter("testOutputFile.txt");
			out.write("Output content.");
			System.out.println("finish.");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (out != null)
			out.close();
		}
		
	}
	
	private static void testArray() {
		int[] a = new int[10];
		for (int i = 0; i < a.length; i++) {
			a[i] = i;
		}
		System.out.println(Arrays.toString(a));
		//Anonymity array.
		System.out.println(Arrays.toString(new int[]{1}));
		//Copy array.
		int[] b = a;
		b[2] = 20;//Now a[2] is also 20.
		System.out.println(Arrays.toString(a));
		
		int[] c = Arrays.copyOf(a, 5);
		System.out.println(Arrays.toString(c));
		
		int[] d = {1, 2, 3, 4, 5, 6};
		int[] e = {11, 22, 33, 44, 55, 66, 77};
		System.arraycopy(d, 1, e, 2, 4);
		System.out.println(Arrays.toString(e));
		
		//Array sort.
		String[] f = {"啊",  "牤", "口", "一", "y", "a", "丁"};
		Arrays.sort(f);
		System.out.println(Arrays.toString(f));
		
	}
	
	/**
	 * 可变参数即是数组
	 * @param args
	 */
	public static void main(String... args) {
//		testArray();
		System.out.println(Integer.toString(01, 10));
	}
}
